package xyz.zhouzhaodong.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import xyz.zhouzhaodong.crm.entity.Material;
import xyz.zhouzhaodong.crm.mapper.MaterialMapper;
import xyz.zhouzhaodong.crm.service.IMaterialService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import xyz.zhouzhaodong.crm.util.ExceptionEnum;
import xyz.zhouzhaodong.crm.util.FileUploadUtil;
import xyz.zhouzhaodong.crm.util.RestMessage;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.beans.Transient;
import java.io.File;
import java.util.*;

/**
 * <p>
 * 材料表 服务实现类
 * </p>
 *
 * @author zhouzhaodong
 * @since 2020-06-13
 */
@Service
public class MaterialServiceImpl extends ServiceImpl<MaterialMapper, Material> implements IMaterialService {

    @Resource
    MaterialMapper materialMapper;

    @Override
    public RestMessage uploadOneFile(MultipartFile file, String staffId, String customerId) {

        // 上传成功
        String fileUrl = FileUploadUtil.uploadOneFile(file, customerId);
        if ("".equals(fileUrl)) {
            return new RestMessage(ExceptionEnum.UPLOAD_FAIL.getCode(), ExceptionEnum.UPLOAD_FAIL.getMsg());
        }
        // 保存上传信息
        saveMaterial(fileUrl, staffId, customerId);
        return new RestMessage(ExceptionEnum.UPLOAD_SUCCESS.getCode(), ExceptionEnum.UPLOAD_SUCCESS.getMsg(), fileUrl);
    }

    @Override
    public RestMessage uploadManyFile(MultipartFile[] files, String staffId, String clientId) {

        // 上传成功
        List<String> list = FileUploadUtil.uploadManyFile(files, clientId);
        if (list.size() < 1 || list.get(0) == null) {
            return new RestMessage(ExceptionEnum.UPLOAD_FAIL.getCode(), ExceptionEnum.UPLOAD_FAIL.getMsg());
        }
        // 循环保存url地址
        for (String str : list) {
            saveMaterial(str, staffId, clientId);
        }
        return new RestMessage(ExceptionEnum.UPLOAD_SUCCESS.getCode(), ExceptionEnum.UPLOAD_SUCCESS.getMsg(), list);
    }

    @Override
    public RestMessage fileDowLoad(HttpServletResponse response, String fileUrl) {
        FileUploadUtil.fileDowLoad(response, fileUrl);
        return RestMessage.success();
    }

    @Override
    public RestMessage findFiles(String customerId) {
        QueryWrapper<Material> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("customer_id", customerId);
        queryWrapper.eq("flag", 0);
        List<Material> list = materialMapper.selectList(queryWrapper);
        return RestMessage.success(list);
    }


    /**
     * 存储信息到数据库
     * @param fileUrl
     * @param staffId
     * @param customerId
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveMaterial(String fileUrl, String staffId, String customerId) {
        // 先删除之前保存的同名的文件数据
        QueryWrapper<Material> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("customer_d", customerId);
        queryWrapper.eq("address", fileUrl);
        queryWrapper.eq("flag", 0);
        materialMapper.delete(queryWrapper);
        // 新增新的文件数据
        Material material = new Material();
        String id = UUID.randomUUID().toString().replaceAll("-", "");
        material.setId(id);
        // 获取文件名称
        String fileName = fileUrl.substring(fileUrl.lastIndexOf("/") + 1);
        material.setName(fileName);
        material.setAddress(fileUrl);
        material.setStaffId(staffId);
        material.setCustomerId(customerId);
        material.setCreateUser(staffId);
        materialMapper.insert(material);
    }

}
